package com.hunttown.mes.common.utils;

import org.apache.velocity.tools.config.DefaultKey;

import java.text.DecimalFormat;

/**
 * created by wangjunfu on 2019-10-17
 */
@DefaultKey("htmlFormat")
public class VelocityHtmlFormat {

    /**
     * 千分位格式化
     *
     * @param text       数字
     * @param isTowPoint 是否保留两位小数 1是0否
     * @return 千分位
     */
    public String fmtMicrometer(String text, int isTowPoint) {

        DecimalFormat df = null;

        if (isTowPoint == 1) {
            df = new DecimalFormat("###,##0.00");
        } else {
            df = new DecimalFormat("###,##0");
        }

        double number = 0.0;
        try {
            number = Double.parseDouble(text);
        } catch (Exception e) {
            number = 0.0;
        }

        return df.format(number);
    }

    //毫秒转【分钟】
    public int fmtMillisecondToMinute(Integer milliseconds) {
        if (milliseconds == null || milliseconds <= 0) {
            return 0;
        }
        return milliseconds / 1000 / 60;
    }

    //毫秒转【**时**分**秒】
    public String fmtMillisecondToHour(Integer milliseconds) {
        if (milliseconds == null || milliseconds <= 0) {
            return "0";
        }

        int minute = milliseconds / 1000 / 60;
        return ConvertUtils.minutesToFormat(minute);
    }

    //米转公里
    public String fmtMeterToKm(Integer meter) {
        float km = (float) meter / 1000;
        return FormatUtils.fmt2point(km);
    }
}
